package cn.tqfeiyang.smartadmin.business.organization.controller;

import cn.tqfeiyang.smartadmin.business.organization.constant.EmployeeConstants;
import cn.tqfeiyang.smartadmin.business.organization.service.EmployeeCurdService;
import cn.tqfeiyang.smartadmin.business.organization.request.EmployeeCreateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.EmployeeUpdateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.EmployeeQueryDTO;
import cn.tqfeiyang.smartadmin.business.organization.response.EmployeeQueryVO;
import cn.tqfeiyang.smartadmin.business.organization.response.EmployeeDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.List;

/**
 * 员工CURD控制器
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@RestController
@Validated
@RequestMapping(EmployeeConstants.REQUEST_URL)
@Tag(name = EmployeeConstants.API_TAG)
public class EmployeeCurdController {

    /**
     * 员工CURD服务
     */
    @Resource
    private EmployeeCurdService employeeCurdService;

    /**
     * 分页查询员工
     */
    @OperationLog
    @GetMapping("/queryEmployees")
    @Operation(summary = "员工 - 分页查询员工")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "queryEmployees")
    public ApiResult<PageData<EmployeeQueryVO>> queryEmployees(@Validated EmployeeQueryDTO dto) {
        return ApiResult.success(employeeCurdService.queryEmployees(dto));
    }

    /**
     * 查询员工详情
     */
    @OperationLog
    @GetMapping("/readEmployee/{employeeId}")
    @Operation(summary = "员工 - 查询员工详情")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "readEmployee")
    public ApiResult<EmployeeDetailVO> readEmployee(@PathVariable @EntityId Long employeeId) {
        return ApiResult.success(employeeCurdService.readEmployee(employeeId));
    }

    /**
     * 添加员工
     */
    @OperationLog
    @PostMapping("/createEmployee")
    @Operation(summary = "员工 - 添加员工")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "createEmployee")
    public ApiResult<EmployeeDetailVO> createEmployee(@RequestBody @Validated EmployeeCreateDTO dto) {
        Long employeeId = employeeCurdService.createEmployee(dto);
        return ApiResult.success(employeeCurdService.readEmployee(employeeId));
    }

    /**
     * 修改员工
     */
    @OperationLog
    @PostMapping("/updateEmployee/{employeeId}")
    @Operation(summary = "员工 - 修改员工")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "updateEmployee")
    public ApiResult<EmployeeDetailVO> updateEmployee(@PathVariable @EntityId Long employeeId, @RequestBody @Validated EmployeeUpdateDTO dto) {
        employeeCurdService.updateEmployee(employeeId, dto);
        return ApiResult.success(employeeCurdService.readEmployee(employeeId));
    }

    /**
     * 删除员工
     */
    @OperationLog
    @PostMapping("/deleteEmployee/{employeeId}")
    @Operation(summary = "员工 - 删除员工")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "deleteEmployee")
    public ApiResult deleteEmployee(@PathVariable @EntityId Long employeeId) {
        employeeCurdService.deleteEmployee(employeeId);
        return ApiResult.success();
    }

    /**
     * 批量删除员工
     */
    @OperationLog
    @PostMapping("/batchDeleteEmployees")
    @Operation(summary = "员工 - 批量删除员工")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "batchDeleteEmployees")
    public ApiResult batchDeleteEmployees(@RequestBody List<Long> employeeIds) {
        employeeCurdService.batchDeleteEmployees(employeeIds);
        return ApiResult.success();
    }
}
